<?php session_start();
?>

<!DOCTYPE html >
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<style>

#loadingimage{
	position:absolute;
	top:32%;
	left:37%;
}
#loading{
display:none;
background: url(../images/loadingback.png) center;
background-size: 100% 100%;
width:100%;
height:100%;
}

.group {
	background: url(../Groups/groupimages/grpsize7.png) center;
	background-size: 100% 100%;
	width: 510px;
	height: 360px;
	text-align: center;
	position: absolute;
	left: 10%;
	top: 15%;
	font-size: 13px;
	color: white;
}

.option1 {
	cursor:pointer;
}

.option2 {
	background-color: #f1f1f1;
}

.list li:HOVER {
	background-color: honeydew;
}

.list li {
	height: 20px;
}

.list {
	background: floralwhite;
	list-style-type: none;
	text-align: left;
	padding: 0px;
	margin: 0px;
	width: 200px;
	border: 1px cyan solid;
	display: none;
}

.listshow {
	border: 1px silver solid;
	box-shadow: 2px 0px 2px silver;
	width: 202px;
}

.listdiv:HOVER ul {
	display: block;
}

.listdiv:HOVER img {
	transform: rotate(180deg);
	-ms-transform: rotate(180deg); /* IE 9 */
	-moz-transform: rotate(180deg); /* Firefox */
	-webkit-transform: rotate(180deg); /* Safari and Chrome */
	-o-transform: rotate(180deg); /* Opera */
}

.listdiv {
	width: 200px;
}
</style>
<script type="text/javascript" src="groupsDisplay.js"></script>
<script type="text/javascript" src="../Student/processCommands.js"></script>
<script>
function changeNames(str)
{
	obj=document.getElementById('loading');
	obj.style.position='absolute';
if (str=="")
  {
  return;
  } 
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
	    obj.style.display='none';
    displayName(xmlhttp.responseText);
    }
  else
  {
	  obj.style.display='block';
  }
  }
xmlhttp.open("GET","getgroup.php?q="+str,true);
xmlhttp.send();
}
</script>
<title>Your Current Group</title>
</head>
<?php 
	$gid=$_SESSION['GID'];
	$pid=$_SESSION['PID'];
	$con=mysql_connect("localhost","root","");
	if(!$con)
	{
		die("Unable to connect to database");
	}
	mysql_select_db("se_group2",$con);
	$result=mysql_query("SELECT *
			FROM `se_group2`.`group_student`,`se_group2`.`group`
			WHERE `group`.`GID`=".$gid."
			AND `group`.`PID`=".$pid."
			AND  `group_student`.`GID` =".$gid."
			AND  `group_student`.`PID` =".$pid);
	$size=0;
	$names=array("","","","","","","","","","");
	$group="";
	$manager="";
	while($row=mysql_fetch_array($result))
	{
		$uid=$row['UID'];
		$muid=$row['owner_id'];
		$res1=mysql_query("SELECT name FROM student WHERE UID=".$uid);
		$row1=mysql_fetch_array($res1);
		$names[$size]=$row1['name'];
		$group.=$row1['name'].'/';
		$res2=mysql_query("SELECT name FROM student WHERE UID=".$muid);
		$row2=mysql_fetch_array($res2);
		$manager=$row2['name'];
		$size=$row['current_size'];
	}
	$group.=$manager.'/'.$size;
	?>
<body onLoad="displayName('<?php echo $group;?>')">
	<br />
	<table style="width:100%">
		<tr>
			<td align="left" valign="top"><input id="Button2" type="button"
				value="Create New Group"
				onclick="alert('You have already created the maximum no of groups possible by a single user. Please dissolve the current group to form a new group!')" /></td>
			<td align="right" valign="top">
				<div class="listdiv">
					<div class="listshow">
						Available Groups<img id="arrow" src="../images/arrow_up.png"
							style="height: 20px; width: 20px; float: right;"></img>
					</div>
					<ul class="list" contenteditable="false">
					<?php
					$res=mysql_query("SELECT GID,current_size,name FROM `se_group2`.`group`,`student` WHERE PID=".$pid." AND owner_id=UID;");
					while($row=mysql_fetch_array($res))
					{
						$GID=$row['GID'];
						$owner=$row['name'];
						echo '<li class="option1" id="'.$GID.'@'.$pid.'" onclick="changeNames(this.id)">Group	'.$GID.' - '.$owner.'</li>';
					} 
					?>
						
					</ul>
				</div>
			</td>
		</tr>
	</table>
	<br />
	<br />
	<br />
	<?php
	$fullName=array();
	$j=0;
	echo '
		<div class="group" id="group" draggable="true" >
		<div id="loading" ><img id="loadingimage" src="../images/loading.gif"></img></div>';
	foreach ($names as $name)
	{
		$fullName[$j]=array(strtok($name," "),strtok(" "));
		echo '<label id=p'.($j+1).' style="position: relative;">
			</label>';
		if($fullName[$j][1]==NULL)
			echo "<br/>";
		$j++;
	}
	echo	'<label id=manager
			style="position: relative; font-weight: bold; font-size: 15px;">
		</label>
		</div>'; 
	?>

	<br />
	<br />
	<table id="table2" style="position: absolute; top: 90%; left: 40%;">
		<tr>
			<td align="center"><input id="Button1" type="button"
				value="Leave this Group"
				onclick="dissolveGroup()"/></td>
		</tr>
	</table>


</body>
</html>
